#ifndef __COMMH_CLASSDB_H__
#define __COMMH_CLASSDB_H__

#include "commclass.h"
#include "CTCodeMySQL.h"

using namespace CTLib;

#ifndef CHECK_ENCODE_RET
#define CHECK_ENCODE_RET(iRet) \
    if (iRet) \
    { \
        CT_WARNING(("MysqlEncodeStruct fail: ret=%d", iRet)); \
        return iRet; \
    }
#endif

class CPlayerBaseInfoDB: public CTCodeObject
{
public:
    CPlayerBaseInfo stPlayerBaseInfo;

    static int BuildSelectSQL(CReqSQL& stReq, const char* szDBName, unsigned int uiGID)
    {
        stReq.Set("select Data from %s.TPlayerBaseInfo where GID = %u", szDBName, uiGID);
        return 0;
    }

    static int BuildSelectGIDSQL(CReqSQL& stReq, const char* szDBName, CTSizeString<USER_TOKEN_LEN>& szUserToken)
    {
        char sBuf[1024] = {0};

        TMySQLCharset::Instance()->EscapeString(sBuf, sizeof(sBuf),
                szUserToken.Get(), szUserToken.GetDataLen());

        stReq.Set("select GID from %s.TPlayerBaseInfo where UserToken = '%s'", szDBName, sBuf);

        return 0;
    }

    static int BuildInsertSQL(CReqSQL& stReq, const char* szDBName, CPlayerBaseInfo& stPlayerBaseInfo, CTBuffer &stEncodeBuf)
    {
        int iRet = 0;
        char sBuf[1024] = {0};

        stReq.Set("insert into %s.TPlayerBaseInfo set ", szDBName);
        stReq.Cat("GID = %u, ", stPlayerBaseInfo._uiGID);

        TMySQLCharset::Instance()->EscapeString(sBuf, sizeof(sBuf),
            stPlayerBaseInfo._sUserToken.Get(), stPlayerBaseInfo._sUserToken.GetDataLen());

        stReq.Cat("UserToken = '%s', ", sBuf);
        stReq.Cat("Gold = %u, ", stPlayerBaseInfo._iGold);
        stReq.Cat("Silver = %lld, ", stPlayerBaseInfo._i64Silver);
        stReq.Cat("RoleLevel = %d, ", stPlayerBaseInfo._shLevel);
        stReq.Cat("PayAmount = %d, ", stPlayerBaseInfo._stVipData._iPayGold);

        stReq.Cat("Data = '");
        iRet = MysqlEncodeStruct(stReq.stSQLMen, stPlayerBaseInfo, stEncodeBuf);
        CHECK_ENCODE_RET(iRet);
        stReq.Cat("'");

        return 0;
    }

    static int BuildUpdateSQL(CReqSQL& stReq, const char* szDBName, CPlayerBaseInfo& stPlayerBaseInfo, CTBuffer &stEncodeBuf)
    {
        int iRet = 0;
        char sBuf[1024] = {0};

        stReq.Set("update %s.TPlayerBaseInfo set ", szDBName);

        TMySQLCharset::Instance()->EscapeString(sBuf, sizeof(sBuf),
            stPlayerBaseInfo._sUserToken.Get(), stPlayerBaseInfo._sUserToken.GetDataLen());

        stReq.Cat("UserToken = '%s', ", stPlayerBaseInfo._sUserToken());
        stReq.Cat("Gold = %u, ", stPlayerBaseInfo._iGold);
        stReq.Cat("Silver = %lld, ", stPlayerBaseInfo._i64Silver);
        stReq.Cat("RoleLevel = %d, ", stPlayerBaseInfo._shLevel);
        stReq.Cat("PayAmount = %d, ", stPlayerBaseInfo._stVipData._iPayGold);

        stReq.Cat("Data = '");
        iRet = MysqlEncodeStruct(stReq.stSQLMen, stPlayerBaseInfo, stEncodeBuf);
        CHECK_ENCODE_RET(iRet);
        stReq.Cat("' ");

        stReq.Cat("where GID = %u ", stPlayerBaseInfo._uiGID);

        return 0;
    }

    static int BuildReplaceSQL(CReqSQL& stReq, const char* szDBName, CPlayerBaseInfo& stPlayerBaseInfo, CTBuffer &stEncodeBuf)
    {
        int iRet = 0;
        char sBuf[1024] = {0};

        stReq.Set("replace into %s.TPlayerBaseInfo set ", szDBName);
        stReq.Cat("GID = %u, ", stPlayerBaseInfo._uiGID);

        TMySQLCharset::Instance()->EscapeString(sBuf, sizeof(sBuf),
            stPlayerBaseInfo._sUserToken.Get(), stPlayerBaseInfo._sUserToken.GetDataLen());
        
        stReq.Cat("UserToken = '%s', ", sBuf);
        stReq.Cat("Gold = %u, ", stPlayerBaseInfo._iGold);
        stReq.Cat("Silver = %lld, ", stPlayerBaseInfo._i64Silver);
        stReq.Cat("RoleLevel = %d, ", stPlayerBaseInfo._shLevel);
        stReq.Cat("PayAmount = %d, ", stPlayerBaseInfo._stVipData._iPayGold);

        stReq.Cat("Data = '");
        iRet = MysqlEncodeStruct(stReq.stSQLMen, stPlayerBaseInfo, stEncodeBuf);
        CHECK_ENCODE_RET(iRet);
        stReq.Cat("'");

        return 0;
    }

    static int BuildSelectMaxGIDSQL(CReqSQL& stReq, const char* szDBName)
    {
        stReq.Set("select ifnull(max(GID %% %d), 0) from %s.TPlayerBaseInfo", MAX_USER_PER_ZONE, szDBName);
        return 0;
    }
};

BEGIN_CODE_CONVERT(CPlayerBaseInfoDB)
CODE_CONVERT(stPlayerBaseInfo)
END_CODE_CONVERT(CPlayerBaseInfoDB)

//////////////////////////////////////////////////////////////////////////

class CPlayerDetailInfoDB: public CTCodeObject
{
public:
    CPlayerDetailInfo stPlayerDetailInfo;

    static int BuildSelectSQL(CReqSQL& stReq, const char* szDBName, unsigned int uiGID)
    {
        stReq.Set("select Data from %s.TPlayerDetailInfo where GID = %u", szDBName, uiGID);
        return 0;
    }

    static int BuildInsertSQL(CReqSQL& stReq, const char* szDBName, CPlayerDetailInfo& stPlayerDetailInfo, CTBuffer &stEncodeBuf)
    {
        int iRet = 0;

        stReq.Set("insert into %s.TPlayerDetailInfo set ", szDBName);
        stReq.Cat("GID = %u, ", stPlayerDetailInfo._uiGID);

        stReq.Cat("Data = '");
        iRet = MysqlEncodeStruct(stReq.stSQLMen, stPlayerDetailInfo, stEncodeBuf);
        CHECK_ENCODE_RET(iRet);
        stReq.Cat("'");

        return 0;
    }

    static int BuildUpdateSQL(CReqSQL& stReq, const char* szDBName, CPlayerDetailInfo& stPlayerDetailInfo, CTBuffer &stEncodeBuf)
    {
        int iRet = 0;

        stReq.Set("update %s.TPlayerDetailInfo set ", szDBName);

        stReq.Cat("Data = '");
        iRet = MysqlEncodeStruct(stReq.stSQLMen, stPlayerDetailInfo, stEncodeBuf);
        CHECK_ENCODE_RET(iRet);
        stReq.Cat("' ");

        stReq.Cat("where GID = %u ", stPlayerDetailInfo._uiGID);

        return 0;
    }

    static int BuildReplaceSQL(CReqSQL& stReq, const char* szDBName, CPlayerDetailInfo& stPlayerDetailInfo, CTBuffer &stEncodeBuf)
    {
        int iRet = 0;

        stReq.Set("replace into %s.TPlayerDetailInfo set ", szDBName);
        stReq.Cat("GID = %u, ", stPlayerDetailInfo._uiGID);

        stReq.Cat("Data = '");
        iRet = MysqlEncodeStruct(stReq.stSQLMen, stPlayerDetailInfo, stEncodeBuf);
        CHECK_ENCODE_RET(iRet);
        stReq.Cat("'");

        return 0;
    }
};

BEGIN_CODE_CONVERT(CPlayerDetailInfoDB)
CODE_CONVERT(stPlayerDetailInfo)
END_CODE_CONVERT(CPlayerDetailInfoDB)

//////////////////////////////////////////////////////////////////////////
class CPlayerDetailInfoEx1DB: public CTCodeObject
{
public:
    CPlayerDetailInfoEx1 stPlayerDetailInfoEx1;

    static int BuildSelectSQL(CReqSQL& stReq, const char* szDBName, unsigned int uiGID)
    {
        stReq.Set("select Data from %s.TPlayerDetailInfoEx1 where GID = %u", szDBName, uiGID);
        return 0;
    }

    static int BuildInsertSQL(CReqSQL& stReq, const char* szDBName,  unsigned int uiGID,  CPlayerDetailInfoEx1& stPlayerDetailInfoEx1, CTBuffer &stEncodeBuf)
    {
        int iRet = 0;

        stReq.Set("insert into %s.TPlayerDetailInfoEx1 set ", szDBName);
        stReq.Cat("GID = %u, ",  uiGID);

        stReq.Cat("Data = '");
        iRet = MysqlEncodeStruct(stReq.stSQLMen, stPlayerDetailInfoEx1, stEncodeBuf);
        CHECK_ENCODE_RET(iRet);
        stReq.Cat("'");

        return 0;
    }

    static int BuildUpdateSQL(CReqSQL& stReq, const char* szDBName, unsigned int uiGID,  CPlayerDetailInfoEx1 & stPlayerDetailInfoEx1, CTBuffer &stEncodeBuf)
    {
        int iRet = 0;

        stReq.Set("update %s.TPlayerDetailInfoEx1 set ", szDBName);

        stReq.Cat("Data = '");
        iRet = MysqlEncodeStruct(stReq.stSQLMen, stPlayerDetailInfoEx1, stEncodeBuf);
        CHECK_ENCODE_RET(iRet);
        stReq.Cat("' ");

        stReq.Cat("where GID = %u ", uiGID);

        return 0;
    }

    static int BuildReplaceSQL(CReqSQL& stReq, const char* szDBName, unsigned int uiGID, CPlayerDetailInfoEx1 & stPlayerDetailInfoEx1,CTBuffer &stEncodeBuf)
    {
        int iRet = 0;

        stReq.Set("replace into %s.TPlayerDetailInfoEx1 set ", szDBName);
        stReq.Cat("GID = %u, ", uiGID);

        stReq.Cat("Data = '");
        iRet = MysqlEncodeStruct(stReq.stSQLMen, stPlayerDetailInfoEx1, stEncodeBuf);
        CHECK_ENCODE_RET(iRet);
        stReq.Cat("'");

        return 0;
    }
};

BEGIN_CODE_CONVERT(CPlayerDetailInfoEx1DB)
CODE_CONVERT(stPlayerDetailInfoEx1)
END_CODE_CONVERT(CPlayerDetailInfoEx1DB)

//////////////////////////////////////////////////////////////////////////
class CPlayerSnsDetailInfoDB: public CTCodeObject
{
public:
    CPlayerSnsDetailInfo stPlayerSnsDetailInfo;

    static int BuildSelectSQL(CReqSQL& stReq, const char* szDBName, unsigned int uiGID)
    {
        stReq.Set("select GID,MyInfo,CommInfo from %s.TPlayerSnsInfo where GID = %u", szDBName, uiGID);
        return 0;
    }

    static int BuildUpdateSQL(CReqSQL& stReq, const char* szDBName, CPlayerSnsDetailInfo& stPlayerSnsDetailInfo, CTBuffer &stEncodeBuf)
    {
        int iRet = 0;

        stReq.Set("update %s.TPlayerSnsInfo set ", szDBName);
        
        stReq.Cat("LastLogInTime = from_unixtime(%d), ", stPlayerSnsDetailInfo._stCommInfo._iLastLogInTime);

        stReq.Cat("MyInfo = '");
        iRet = MysqlEncodeStruct(stReq.stSQLMen, stPlayerSnsDetailInfo._stMyInfo, stEncodeBuf);
        CHECK_ENCODE_RET(iRet);
        stReq.Cat("', ");

        stReq.Cat("CommInfo = '");
        iRet = MysqlEncodeStruct(stReq.stSQLMen, stPlayerSnsDetailInfo._stCommInfo, stEncodeBuf);
        CHECK_ENCODE_RET(iRet);
        stReq.Cat("' ");

        stReq.Cat("where GID = %u ", stPlayerSnsDetailInfo._uiGID);

        return 0;
    }

    static int BuildInsertSQL(CReqSQL& stReq, const char* szDBName, CPlayerSnsDetailInfo& stPlayerSnsDetailInfo, CTBuffer &stEncodeBuf)
    {
        int iRet = 0;

        stReq.Set("insert into %s.TPlayerSnsInfo set ", szDBName);

        stReq.Cat("LastLogInTime = from_unixtime(%d), ", stPlayerSnsDetailInfo._stCommInfo._iLastLogInTime);

        stReq.Cat("MyInfo = '");
        iRet = MysqlEncodeStruct(stReq.stSQLMen, stPlayerSnsDetailInfo._stMyInfo, stEncodeBuf);
        CHECK_ENCODE_RET(iRet);
        stReq.Cat("', ");

        stReq.Cat("CommInfo = '");
        iRet = MysqlEncodeStruct(stReq.stSQLMen, stPlayerSnsDetailInfo._stCommInfo, stEncodeBuf);
        CHECK_ENCODE_RET(iRet);
        stReq.Cat("', ");

        stReq.Cat("GID = %u ", stPlayerSnsDetailInfo._uiGID);

        return 0;
    }

    static int BuildReplaceSQL(CReqSQL& stReq, const char* szDBName, CPlayerSnsDetailInfo& stPlayerSnsDetailInfo, CTBuffer &stEncodeBuf)
    {
        int iRet = 0;

        stReq.Set("replace into %s.TPlayerSnsInfo set ", szDBName);

        stReq.Cat("LastLogInTime = from_unixtime(%d), ", stPlayerSnsDetailInfo._stCommInfo._iLastLogInTime);

        stReq.Cat("MyInfo = '");
        iRet = MysqlEncodeStruct(stReq.stSQLMen, stPlayerSnsDetailInfo._stMyInfo, stEncodeBuf);
        CHECK_ENCODE_RET(iRet);
        stReq.Cat("', ");

        stReq.Cat("CommInfo = '");
        iRet = MysqlEncodeStruct(stReq.stSQLMen, stPlayerSnsDetailInfo._stCommInfo, stEncodeBuf);
        CHECK_ENCODE_RET(iRet);
        stReq.Cat("', ");

        stReq.Cat("GID = %u ", stPlayerSnsDetailInfo._uiGID);

        return 0;
    }
};

//////////////////////////////////////////////////////////////////////////
class CPlayerSnsCommInfoDB: public CTCodeObject
{
public:
    enum
    {
        SELECT_PLAYER_SNS_COMM_INFO_COUNT_PER_TIME = 100,  //一次获取的记录条数
    };

    CPlayerSnsCommInfo stPlayerSnsCommInfo;

    static int BuildSelectSQL(CReqSQL& stReq, const char* szDBName, int iFrom)
    {
        stReq.Set("select CommInfo from %s.TPlayerSnsInfo limit %d, %d", szDBName,
                                                       iFrom, SELECT_PLAYER_SNS_COMM_INFO_COUNT_PER_TIME);
        return 0;
    }

    static int BuildSelectCountSQL(CReqSQL& stReq, const char* szDBName)
    {
        stReq.Set("select count(*) from %s.TPlayerSnsInfo", szDBName);
        return 0;
    }
};

//////////////////////////////////////////////////////////////////////////
class CUnactPartnerInfoDB: public CTCodeObject
{
public:
    CUnactPartnerInfo stUnactPartnerInfo;

    static int BuildSelectSQL(CReqSQL& stReq, const char* szDBName, unsigned int uiGID)
    {
        stReq.Set("select Data from %s.TUnactPartnerInfo where GID = %u", szDBName, uiGID);
        return 0;
    }

    static int BuildUpdateSQL(CReqSQL& stReq, const char* szDBName, CUnactPartnerInfo& stUnactPartnerInfo, CTBuffer &stEncodeBuf)
    {
        int iRet = 0;

        stReq.Set("update %s.TUnactPartnerInfo set ", szDBName);

        stReq.Cat("Data = '");
        iRet = MysqlEncodeStruct(stReq.stSQLMen, stUnactPartnerInfo, stEncodeBuf);
        CHECK_ENCODE_RET(iRet);
        stReq.Cat("' ");

        stReq.Cat("where GID = %u ", stUnactPartnerInfo._uiGID);

        return 0;
    }

    static int BuildInsertSQL(CReqSQL& stReq, const char* szDBName, CUnactPartnerInfo& stUnactPartnerInfo, CTBuffer &stEncodeBuf)
    {
        int iRet = 0;

        stReq.Set("insert into %s.TUnactPartnerInfo set ", szDBName);

        stReq.Cat("Data = '");
        iRet = MysqlEncodeStruct(stReq.stSQLMen, stUnactPartnerInfo, stEncodeBuf);
        CHECK_ENCODE_RET(iRet);
        stReq.Cat("', ");

        stReq.Cat("GID = %u ", stUnactPartnerInfo._uiGID);

        return 0;
    }

    static int BuildReplaceSQL(CReqSQL& stReq, const char* szDBName, CUnactPartnerInfo& stUnactPartnerInfo, CTBuffer &stEncodeBuf)
    {
        int iRet = 0;

        stReq.Set("replace into %s.TUnactPartnerInfo set ", szDBName);

        stReq.Cat("Data = '");
        iRet = MysqlEncodeStruct(stReq.stSQLMen, stUnactPartnerInfo, stEncodeBuf);
        CHECK_ENCODE_RET(iRet);
        stReq.Cat("', ");

        stReq.Cat("GID = %u ", stUnactPartnerInfo._uiGID);

        return 0;
    }

};

//////////////////////////////////////////////////////////////////////////
class CArenaRankDB: public CTCodeObject
{
public:
    enum
    {
        SELECT_ARENA_RANK_COUNT_PER_TIME = 100,  //一次获取的记录条数
    };

    CArenaRank stArenaRank;

    static int BuildSelectCountSQL(CReqSQL& stReq, const char* szDBName)
    {
        stReq.Set("select count(*) from %s.TArenaRank", szDBName);
        return 0;
    }

    static int BuildSelectSQL(CReqSQL& stReq, const char* szDBName, int iFrom)
    {
        stReq.Set("select * from %s.TArenaRank order by NowRank limit %d, %d", szDBName,
            iFrom, SELECT_ARENA_RANK_COUNT_PER_TIME);
        return 0;
    }

    static int BuildUpdateSQL(CReqSQL& stReq, const char* szDBName, CArenaRank& stArenaRank, CTBuffer &stEncodeBuf)
    {
        stReq.Set("update %s.TArenaRank set ", szDBName);

        stReq.Cat("MaxRank = %d, ", stArenaRank._iMaxRank);
        stReq.Cat("BonusRank = %d, ", stArenaRank._iBonusRank);
        stReq.Cat("NowRank = %d, ", stArenaRank._iNowRank);
        stReq.Cat("NowRankUpdateTime = %d, ", stArenaRank._iNowRankUpdateTime);
        stReq.Cat("ConWinCount = %d, ", stArenaRank._iConWinCount);
        stReq.Cat("LastWinTime = %d ", stArenaRank._iLastWinTime);
        stReq.Cat("RankUpCount = %d, ", stArenaRank._iRankUpCount);
        stReq.Cat("where GID = %u ", stArenaRank._uiGID);

        return 0;
    }

    static int BuildInsertSQL(CReqSQL& stReq, const char* szDBName, CArenaRank& stArenaRank, CTBuffer &stEncodeBuf)
    {
        stReq.Set("insert into %s.TArenaRank set ", szDBName);

        stReq.Cat("MaxRank = %d, ", stArenaRank._iMaxRank);
        stReq.Cat("BonusRank = %d, ", stArenaRank._iBonusRank);
        stReq.Cat("NowRank = %d, ", stArenaRank._iNowRank);
        stReq.Cat("NowRankUpdateTime = %d, ", stArenaRank._iNowRankUpdateTime);
        stReq.Cat("ConWinCount = %d, ", stArenaRank._iConWinCount);
        stReq.Cat("LastWinTime = %d, ", stArenaRank._iLastWinTime);
        stReq.Cat("RankUpCount = %d, ", stArenaRank._iRankUpCount);
        stReq.Cat("GID = %u ", stArenaRank._uiGID);

        return 0;
    }

    static int BuildReplaceSQL(CReqSQL& stReq, const char* szDBName, CArenaRank& stArenaRank, CTBuffer &stEncodeBuf)
    {
        stReq.Set("replace into %s.TArenaRank set ", szDBName);

        stReq.Cat("MaxRank = %d, ", stArenaRank._iMaxRank);
        stReq.Cat("BonusRank = %d, ", stArenaRank._iBonusRank);
        stReq.Cat("NowRank = %d, ", stArenaRank._iNowRank);
        stReq.Cat("NowRankUpdateTime = %d, ", stArenaRank._iNowRankUpdateTime);
        stReq.Cat("ConWinCount = %d, ", stArenaRank._iConWinCount);
        stReq.Cat("LastWinTime = %d, ", stArenaRank._iLastWinTime);
        stReq.Cat("RankUpCount = %d, ", stArenaRank._iRankUpCount);
        stReq.Cat("GID = %u ", stArenaRank._uiGID);

        return 0;
    }

};

//////////////////////////////////////////////////////////////////////////
class CZoneDataDB: public CTCodeObject
{
public:
    static int BuildSelectSQL(CReqSQL& stReq, const char* szDBName, unsigned int uiZoneID)
    {
        stReq.Set("select Data from %s.TZoneData where ZoneID = %u", szDBName, uiZoneID);
        return 0;
    }

    static int BuildSelectCountSQL(CReqSQL& stReq, const char* szDBName)
    {
        stReq.Set("select count(*) from %s.TZoneData", szDBName);
        return 0;
    }

    static int BuildUpdateSQL(CReqSQL& stReq, const char* szDBName, CZoneData& stZoneData, CTBuffer &stEncodeBuf)
    {
        int iRet = 0;
        stReq.Set("update %s.TZoneData set ", szDBName);

        stReq.Cat("Data = '");
        iRet = MysqlEncodeStruct(stReq.stSQLMen, stZoneData, stEncodeBuf);
        CHECK_ENCODE_RET(iRet);
        stReq.Cat("' ");

        stReq.Cat("where ZoneID = %u ", stZoneData._uiZoneID);

        return 0;
    }

    static int BuildInsertSQL(CReqSQL& stReq, const char* szDBName, CZoneData& stZoneData, CTBuffer &stEncodeBuf)
    {
        int iRet = 0;
        stReq.Set("insert into %s.TZoneData set ", szDBName);

        stReq.Cat("Data = '");
        iRet = MysqlEncodeStruct(stReq.stSQLMen, stZoneData, stEncodeBuf);
        CHECK_ENCODE_RET(iRet);
        stReq.Cat("', ");

        stReq.Cat("ZoneID = %u ", stZoneData._uiZoneID);

        return 0;
    }

    static int BuildReplaceSQL(CReqSQL& stReq, const char* szDBName, CZoneData& stZoneData, CTBuffer &stEncodeBuf)
    {
        int iRet = 0;
        stReq.Set("replace into %s.TZoneData set ", szDBName);

        stReq.Cat("Data = '");
        iRet = MysqlEncodeStruct(stReq.stSQLMen, stZoneData, stEncodeBuf);
        CHECK_ENCODE_RET(iRet);
        stReq.Cat("', ");

        stReq.Cat("ZoneID = %u ", stZoneData._uiZoneID);

        return 0;
    }

};

//////////////////////////////////////////////////////////////////////////
class CClanDataDB: public CTCodeObject
{
public:
    enum
    {
        SELECT_CLAN_DATA_COUNT_PER_TIME = 10,  //一次获取的记录条数
    };

    static int BuildSelectSQL(CReqSQL& stReq, const char* szDBName, int iFrom)
    {
        stReq.Set("select Data from %s.TClanData limit %d, %d", szDBName, iFrom, SELECT_CLAN_DATA_COUNT_PER_TIME);
        return 0;
    }

    static int BuildUpdateSQL(CReqSQL& stReq, const char* szDBName, CClanData& stClanData, CTBuffer &stEncodeBuf)
    {
        int iRet = 0;
        stReq.Set("update %s.TClanData set ", szDBName);
        
        stReq.Cat("ClanLevel = %d, ", stClanData._ucClanLevel);
        stReq.Cat("MemberCount = %d, ", stClanData._astClanMember.Size());
        stReq.Cat("Construction = %d, ", stClanData._iConstruction);

        stReq.Cat("Data = '");
        iRet = MysqlEncodeStruct(stReq.stSQLMen, stClanData, stEncodeBuf);
        CHECK_ENCODE_RET(iRet);
        stReq.Cat("' ");

        stReq.Cat("where ClanID = %u ", stClanData._uiClanID);

        return 0;
    }

    static int BuildInsertSQL(CReqSQL& stReq, const char* szDBName, CClanData& stClanData, CTBuffer &stEncodeBuf)
    {
        int iRet = 0;
        stReq.Set("insert into %s.TClanData set ", szDBName);

        stReq.Cat("ClanLevel = %d, ", stClanData._ucClanLevel);
        stReq.Cat("MemberCount = %d, ", stClanData._astClanMember.Size());
        stReq.Cat("Construction = %d, ", stClanData._iConstruction);

        stReq.Cat("Data = '");
        iRet = MysqlEncodeStruct(stReq.stSQLMen, stClanData, stEncodeBuf);
        CHECK_ENCODE_RET(iRet);
        stReq.Cat("', ");

        stReq.Cat("ClanID = %u ", stClanData._uiClanID);

        return 0;
    }

    static int BuildReplaceSQL(CReqSQL& stReq, const char* szDBName, CClanData& stClanData, CTBuffer &stEncodeBuf)
    {
        int iRet = 0;
        stReq.Set("replace into %s.TClanData set ", szDBName);

        stReq.Cat("ClanLevel = %d, ", stClanData._ucClanLevel);
        stReq.Cat("MemberCount = %d, ", stClanData._astClanMember.Size());
        stReq.Cat("Construction = %d, ", stClanData._iConstruction);

        stReq.Cat("Data = '");
        iRet = MysqlEncodeStruct(stReq.stSQLMen, stClanData, stEncodeBuf);
        CHECK_ENCODE_RET(iRet);
        stReq.Cat("', ");

        stReq.Cat("ClanID = %u ", stClanData._uiClanID);

        return 0;
    }

    static int BuildDeleteSQL(CReqSQL& stReq, const char* szDBName, unsigned int uiClanID)
    {
        stReq.Set("delete from %s.TClanData where ClanID = %u", szDBName, uiClanID);
        return 0;
    }

    static int BuildSelectCountSQL(CReqSQL& stReq, const char* szDBName)
    {
        stReq.Set("select count(*) from %s.TClanData", szDBName);
        return 0;
    }

};

//////////////////////////////////////////////////////////////////////////
class CPlayerOssDataDB
{
public:
    static int BuildReplaceSQL(CReqSQL& stReq, const char* szDBName, CPlayerOssData& stPlayerOssData, CTBuffer &stEncodeBuf)
    {
        stReq.Set("replace into %s.TOssPlayerData set ", szDBName);

        stReq.Cat("UpdateDate = from_unixtime(%d), ", stPlayerOssData._iUpdateTime);
        stReq.Cat("GID = %d, ", stPlayerOssData._uiGID);
        stReq.Cat("HasPaied = %d, ", stPlayerOssData._ucHasPaied);
        stReq.Cat("NowLevel = %d, ", stPlayerOssData._usNowLevel);
        stReq.Cat("FirstPayTime = from_unixtime(%d), ", stPlayerOssData._iFirstPayTime);
        stReq.Cat("FirstPayFee = %d, ", stPlayerOssData._iFirstPayFee);
        stReq.Cat("RegTime = from_unixtime(%d), ", stPlayerOssData._iRegTime);
        stReq.Cat("LoginTime = from_unixtime(%d), ", stPlayerOssData._iLoginTime);
        stReq.Cat("ActTime = from_unixtime(%d), ", stPlayerOssData._iActTime);
        stReq.Cat("TodayLoginSec = %d ", stPlayerOssData._iTodayLoginSec);

        return 0;
    }
};

//////////////////////////////////////////////////////////////////////////
class CPlayerOssMoneyDB
{
public:
    static int BuildReplaceSQL(CReqSQL& stReq, const char* szDBName, CPlayerOssMoney & stPlayerOssMoney, CTBuffer &stEncodeBuf)
    {
        stReq.Set("replace into %s.TOssPlayerMoney set ", szDBName);

        stReq.Cat("UpdateDate = from_unixtime(%d), ", stPlayerOssMoney._iUpdateTime);
        stReq.Cat("GID = %d, ", stPlayerOssMoney._uiGID);
        stReq.Cat("Opt = %d, ", stPlayerOssMoney._ucOpt);
        stReq.Cat("Module = %d, ", stPlayerOssMoney._usModule);
        stReq.Cat("Amount = %d ", stPlayerOssMoney._iAmount);

        return 0;
    }
};

//////////////////////////////////////////////////////////////////////////
class COssZoneDataDB
{
public:
    static int BuildReplaceSQL(CReqSQL& stReq, const char* szDBName, COssZoneData& stOssZoneData, CTBuffer &stEncodeBuf)
    {
        stReq.Set("replace into %s.TOssZoneData set ", szDBName);

        stReq.Cat("UpdateDate = from_unixtime(%d), ", stOssZoneData._iUpdateTime);
        stReq.Cat("ZoneID = %d, ", stOssZoneData._uiZoneID);
        stReq.Cat("NowOnline = %d, ", stOssZoneData._iNowOnline);
        stReq.Cat("QuarterRegister = %d, ", stOssZoneData._iQuarterRegister);
        stReq.Cat("QuarterIncome = %d ", stOssZoneData._uiQuarterIncome);

        return 0;
    }
};

//////////////////////////////////////////////////////////////////////////
class CInstanceZoneDetailInfoDB: public CTCodeObject
{
public:
    static int BuildSelectSQL(CReqSQL& stReq, const char* szDBName, int iID)
    {
        stReq.Set("select Data from %s.TInstanceZoneDetailInfo where ID = %d", szDBName, iID);
        return 0;
    }

    static int BuildReplaceSQL(CReqSQL& stReq, const char* szDBName, CInstanceZoneDetailInfo& stData, CTBuffer &stEncodeBuf)
    {
        int iRet = 0;
        stReq.Set("replace into %s.TInstanceZoneDetailInfo set ", szDBName);

        stReq.Cat("Data = '");
        iRet = MysqlEncodeStruct(stReq.stSQLMen, stData, stEncodeBuf);
        CHECK_ENCODE_RET(iRet);
        stReq.Cat("', ");

        stReq.Cat("ID = %d ", stData.GetKey());

        return 0;
    }
};

#endif //__COMMH_CLASSDB_H__

